﻿using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

using Anmlz.Model.Model;

namespace Anmlz.Web.Controllers
{
    using Anmlz.Data.Contracts;

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class CreatureTemperamentsController : BaseApiController
    {
        public CreatureTemperamentsController(IUnitOfWork unitOfWork, IModelFactory modelFactory)
            : base(unitOfWork, modelFactory)
        {
        }

        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        public HttpResponseMessage Get(string name)
        {
            try
            {
                var entity = ModelFactory.Parse(new CreatureTemperamentModel() { Name = name });
                if (entity == null)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }
                var model = ModelFactory.Create(entity);
                return Request.CreateResponse(HttpStatusCode.Found, model);
            }
            catch
            {
                // TODO Add Logging
            }
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        public HttpResponseMessage Post([FromBody]CreatureTemperamentModel breed)
        {
            try
            {
                var entity = ModelFactory.Parse(breed, true);

                if (UnitOfWork.CreatureTemperaments.Insert(entity) && UnitOfWork.Save())
                {
                    return Request.CreateResponse(HttpStatusCode.Created, ModelFactory.Create(entity));
                }
            }
            catch
            {
                // TODO Add Logging
            }
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
}
